<template>
    <div>
        <el-container>
            <el-header class="homeHeader">
                <div style="display: flex">
                    <div class="title">公司：</div>
                    <div style="margin-left: 0;float: left">
                        <el-select v-model="value" placeholder="请选择所属公司" @change="select(value)">
                            <el-option
                                    v-for="item in options"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </div>
                </div>


                <div>
                    <el-button icon="el-icon-bell" type="text" style="margin-right: 8px;color: white;" size="normal"
                               @click="goChat"></el-button>
                    <el-dropdown class="userInfo" @command="commandHandler">
  <span class="el-dropdown-link" style="color:white;">
    {{userData.user.archive.name}}<i class="el-icon-user-solid"></i>
  </span>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item command="userinfo">个人中心</el-dropdown-item>
                            <el-dropdown-item command="setting">设置</el-dropdown-item>
                            <el-dropdown-item command="logout" divided>注销登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </div>
            </el-header>
            <el-container>
                <el-aside width="200px">
                    <el-menu router unique-opened>
                        <el-submenu :index="index+''" v-for="(item,index) in routes" v-if="!item.hidden" :key="index">
                            <template slot="title">
                                <i style="color: #409eff;margin-right: 5px" :class="item.iconCls"></i>
                                <span>{{item.name}}</span>
                            </template>
                            <el-menu-item :index="child.path" v-for="(child,indexj) in item.children" :key="indexj">
                                {{child.name}}
                            </el-menu-item>
                        </el-submenu>
                    </el-menu>
                </el-aside>
                <el-main>
                    <el-breadcrumb separator-class="el-icon-arrow-right" v-if="this.$router.currentRoute.path!='/home'">
                        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                        <el-breadcrumb-item>{{this.$router.currentRoute.name}}</el-breadcrumb-item>
                    </el-breadcrumb>
                    <div class="homeWelcome" v-if="this.$router.currentRoute.path=='/home'">
                        欢迎来到微人事！
                    </div>
                    <router-view class="homeRouterView"/>
                </el-main>
            </el-container>
        </el-container>

        <!--        设置-修改密码-->
        <el-dialog
                :title="title"
                :visible.sync="dialogVisibleMdfPdw"
                width="40%">
            <div>

                <div class="demo-input-suffix" style="margin: 20px 30%">
                    原密码：
                    <el-input
                            placeholder="请输入原密码"
                            v-model="mdfpwd.oldpwd"
                            style="width: 300px"
                    >
                    </el-input>
                </div>
                <div class="demo-input-suffix" style="margin: 20px 30%">
                    新密码：
                    <el-input
                            placeholder="请输入新密码"
                            v-model="mdfpwd.newpwd"
                            style="width: 300px"
                            show-password
                    >
                    </el-input>
                </div>
                <div class="demo-input-suffix" style="margin: 20px 30%">
                    确认密码：
                    <el-input
                            placeholder="请输入确认密码"
                            v-model="mdfpwd.checkpwd"
                            style="width: 300px"
                            show-password
                    >
                    </el-input>
                </div>

            </div>

            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisibleMdfPdw = false">取 消</el-button>
                <el-button type="primary" @click="editMdfPwd">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
    import axios from "axios";
    import {Message} from "element-ui";

    export default {
        name: "Home",
        data() {
            return {
                // user: JSON.parse(window.sessionStorage.getItem("user"))

                // options: [{
                //     value: 1,
                //     label: '黄金糕'
                // }, {
                //     value: 2,
                //     label: '双皮奶'
                // }, {
                //     value: 3,
                //     label: '蚵仔煎'
                // }, {
                //     value: 4,
                //     label: '龙须面'
                // }, {
                //     value: 5,
                //     label: '北京烤鸭'
                // }],
                options: [],
                value: null,
                userData: {},

                dialogVisibleMdfPdw: false,
                title: '',
                mdfpwd: {
                    oldpwd: null,
                    newpwd: null,
                    checkpwd: null,
                },
            }
        },
        created() {

            let u = JSON.parse(window.sessionStorage.getItem("user"));
            this.userData = u;
            // console.log(this.user);
            u.user.archive.positions.forEach(item => {
                this.options.push({
                    value: item.company.id,
                    label: item.company.name,
                })
            })
            // console.log(this.options);
            this.value = JSON.parse(window.sessionStorage.getItem("currentCid"));
            // console.log(this.value);

        },
        computed: {
            routes() {
                return this.$store.state.routes;
            },
            user() {
                return this.$store.state.currentHr;
            }
        },
        methods: {
            goChat() {
                this.$router.push("/chat");
            },
            commandHandler(cmd) {
                if (cmd == 'logout') {
                    this.$confirm('此操作将注销登录, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.getRequest("/logout");
                        window.sessionStorage.removeItem("user")
                        this.$store.commit('initRoutes', []);
                        this.$router.replace("/");
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消操作'
                        });
                    });
                } else if (cmd == 'userinfo') {
                    this.$router.push('/hrinfo');
                } else if (cmd == 'setting') {
                    this.showMdfPwd();
                }
            },
            select(value) {
                this.value = value;
                // console.log(this.value);
                // 选择后重置全局变量为当前公司id
                window.sessionStorage.setItem("currentCid", this.value);
                // console.log(JSON.parse(window.sessionStorage.getItem("currentCid")));
                // console.log(666666666);
                // 刷新
                location.reload();
            },

            showMdfPwd() {
                this.title = '修改密码';
                this.dialogVisibleMdfPdw = true;
                console.log(this.dialogVisibleMdfPdw);

            },
            editMdfPwd() {
                let url = '/pw/put';
                //替换成当前的cid
                let u = JSON.parse(window.sessionStorage.getItem("user"));
                if (this.mdfpwd.oldpwd != u.user.password) {
                    Message.error({message: '原密码不正确！'})
                } else if (this.mdfpwd.newpwd != this.mdfpwd.checkpwd) {
                    Message.error({message: '新密码两次输入不同！'})
                } else if (this.mdfpwd.newpwd == null) {
                    Message.error({message: '新密码不能为空！'})
                } else {
                    return axios({
                        method: 'PUT',
                        url: `${url}`,
                        data: {
                            'uid': u.user.id,
                            'oldpass': this.mdfpwd.oldpwd,
                            'pass': this.mdfpwd.newpwd,
                        }
                    })
                }

                this.dialogVisibleMdfPdw = false;
            }
        }
    }
</script>

<style>
    .homeRouterView {
        margin-top: 10px;
    }

    .homeWelcome {
        text-align: center;
        font-size: 30px;
        font-family: 华文行楷;
        color: #409eff;
        padding-top: 50px;
    }

    .homeHeader {
        background-color: #409eff;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0px 15px;
        box-sizing: border-box;
    }

    .homeHeader .title {
        font-size: 20px;
        font-family: 微软雅黑;
        color: #ffffff
    }

    .homeHeader .userInfo {
        cursor: pointer;
    }

    .el-dropdown-link img {
        width: 48px;
        height: 48px;
        border-radius: 24px;
        margin-left: 8px;
    }

    .el-dropdown-link {
        display: flex;
        align-items: center;
    }
</style>